package com.tao.base.model;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class PageParams {
    @Schema(description = "页码")
    private Long pageNo = 1L;
    @Schema(description = "每页记录数")
    private Long pageSize = 30L;
    @Schema(description = "排序字段",example = "change_date")
    private String sortBy = "change_date";
    @Schema(description = "是否升序",example = "true")
    private Boolean isAsc = true;

    public <T> Page<T> toPage() {
        Page<T> page = Page.of(pageNo, pageSize);
        if (isAsc.equals(true))
            page.addOrder(OrderItem.asc(sortBy));
        else
            page.addOrder(OrderItem.desc(sortBy));
        return page;
    }
}
